package org.budo.support.servlet.filter.origin.cross;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

import org.budo.support.lang.util.StringUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author li
 */
public abstract class AbstractBudoCrossOriginFilter implements Filter {
    private static final Logger log = Slf4j.getLogger();

    public static final String HTTP_METHOD_OPTIONS = "OPTIONS";

    public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";

    public static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers";

    private String accessControlAllowOrigin = "*";

    private String accessControlAllowHeaders = "X-Requested-With,Content-Type";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String accessControlAllowOrigin = filterConfig.getInitParameter(ACCESS_CONTROL_ALLOW_ORIGIN);
        if (!StringUtil.isEmpty(accessControlAllowOrigin)) {
            this.accessControlAllowOrigin = accessControlAllowOrigin;
        }

        String accessControlAllowHeaders = filterConfig.getInitParameter(ACCESS_CONTROL_ALLOW_HEADERS);
        if (!StringUtil.isEmpty(accessControlAllowHeaders)) {
            this.accessControlAllowHeaders = accessControlAllowHeaders;
        }

        log.warn("init, accessControlAllowOrigin=" + accessControlAllowOrigin + ", accessControlAllowHeaders=" + accessControlAllowHeaders + ", filterConfig=" + filterConfig + ", this=" + this);
    }

    @Override
    public void destroy() {
        log.warn("destroy, this=" + this);
    }

    public String getAccessControlAllowHeaders() {
        return accessControlAllowHeaders;
    }

    public String getAccessControlAllowOrigin() {
        return accessControlAllowOrigin;
    }
}